﻿//Description:推送给BPM系统专用服务
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer: 
//create Date:
//Rewriter:
//Rewrite Date:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using UFC.Common;
using System.Configuration;

namespace SendCommissioningData
{
    public partial class SendCommissioningData : ServiceBase
    {
        private System.Timers.Timer m_ApsInfoToBPMTimer;//推送APS排产任务到BPM系统

        public SendCommissioningData()
        {
            InitializeComponent();
        }
        private System.Timers.Timer m_SyncTimer;
        protected override void OnStart(string[] args)
        {
            LogHelp.SetConfig();
            int intSync = Convert.ToInt32(ConfigurationManager.AppSettings["sync"].ToString());

            SyncTimerStatr(ref m_SyncTimer, m_SyncTimer_Elapsed, intSync * 10 * 1000); 

            SyncTimerStatr(ref m_ApsInfoToBPMTimer, m_ApsInfoToBPMTimer_Elapsed, intSync * 10 * 1000);//aps信息推给BPM
        }

        /// <summary>
        /// 传参启动新线程并运行方法
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="m_timerElapsed"></param>
        private void SyncTimerStatr(ref System.Timers.Timer timer, Action<object, System.Timers.ElapsedEventArgs> m_timerElapsed, int intSync)
        {
            timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timerElapsed);

            timer.Interval = intSync;
            timer.Enabled = true;
            timer.Start();
        }

        private void m_SyncTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                LogHelp.WriteLog("SendCommissioningData定时器启动");
                m_SyncTimer.Enabled = false;
                SendCommissioningDataMethod runMain = new SendCommissioningDataMethod();
                runMain.RunMain(); 
            }
            catch (Exception myError)
            {
                LogHelp.WriteError(myError.Message, myError);
            }

            m_SyncTimer.Enabled = true;

        }

        private void m_ApsInfoToBPMTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
               // LogHelp.WriteLog("SendApsAndReportToBPM定时器启动");
                m_ApsInfoToBPMTimer.Enabled = false;
                SendApsAndReportToBPM runMain = new SendApsAndReportToBPM();
                runMain.RunMain();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError(myError.Message, myError);
            }
            finally
            {
                m_ApsInfoToBPMTimer.Enabled = true;                
            }
        }

        protected override void OnStop()
        {
        }
    }
}
